package com.manlitech.elkboot.service.es;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.IndicesClient;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

/**
 * @author Shensg
 */
@Service
@Slf4j
public class EsService {

    Logger logger = LoggerFactory.getLogger(EsService.class);

    @Autowired
    RestHighLevelClient restHighLevelClient;

    /**
     * 查询索引是否存在
     * @return
     * @throws IOException
     */
    public boolean existIndex(String indexName) throws IOException {
        GetIndexRequest request = new GetIndexRequest(indexName);
        boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
        return exists;
    }

    /**
     * 创建索引
     */
    public boolean addIndex(String indexName) throws IOException {
        //1.使用client获取操作索引对象
        IndicesClient indices = restHighLevelClient.indices();
        //2.具体操作获取返回值
        //2.1 设置索引名称
        CreateIndexRequest createIndexRequest=new CreateIndexRequest(indexName);
        //2.2创建先查询
        if(existIndex(indexName)){
           return true;
        }
        CreateIndexResponse createIndexResponse = indices.create(createIndexRequest, RequestOptions.DEFAULT);
        //3.根据返回值判断结果
        if(createIndexResponse.isAcknowledged()){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 添加索引，并添加映射
     */
    public boolean addIndexAndMapping(String indexName , String mappingJson) throws IOException {
        //1.使用client获取操作索引对象
        IndicesClient indices = restHighLevelClient.indices();
        //2.具体操作获取返回值
        //2.具体操作，获取返回值
        CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName);
        //2.1 设置mappings
        if(existIndex(indexName)){
            return true;
        }
        createIndexRequest.mapping(mappingJson, XContentType.JSON);
        CreateIndexResponse createIndexResponse = indices.create(createIndexRequest, RequestOptions.DEFAULT);
        //3.根据返回值判断结果
        if(createIndexResponse.isAcknowledged()){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 删除索引
     */
    public boolean deleteIndex(String indexName) throws IOException {
        IndicesClient indices = restHighLevelClient.indices();
        DeleteIndexRequest deleteRequest=new DeleteIndexRequest(indexName);
        AcknowledgedResponse delete = indices.delete(deleteRequest, RequestOptions.DEFAULT);
        if(delete.isAcknowledged()){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 添加文档,使用map作为数据
     * @param indexName
     * @param id
     * @param map
     * @return
     * @throws IOException
     */
    public String addDocMap(String indexName , String id , Map map) throws IOException {
        IndexRequest request=new IndexRequest(indexName).id(id).source(map);
        IndexResponse response = restHighLevelClient.index(request, RequestOptions.DEFAULT);
        return response.getId();
    }

    /**
     * 修改文档：添加文档时，如果id存在则修改，id不存在则添加
     */
    public String updateDocMap(String indexName , String id , Map map) throws IOException {
        IndexRequest request=new IndexRequest(indexName).id(id).source(map);
        IndexResponse response = restHighLevelClient.index(request, RequestOptions.DEFAULT);
        return response.getId();
    }

    /**
     * 添加文档,使用json字符串作为数据
     * @param indexName
     * @param id
     * @param jsonStr
     * @throws IOException
     */
    public String addDocJson(String indexName , String id , String jsonStr) throws IOException {
        IndexRequest request=new IndexRequest(indexName).id(id).source(jsonStr,XContentType.JSON);
        IndexResponse response = restHighLevelClient.index(request, RequestOptions.DEFAULT);
        return response.getId();
    }

    /**
     * 修改文档：添加文档时，如果id存在则修改，id不存在则添加
     */
    public String UpdateDocJson(String indexName , String id , String jsonStr) throws IOException {
        IndexRequest request=new IndexRequest(indexName).id(id).source(jsonStr,XContentType.JSON);
        IndexResponse response = restHighLevelClient.index(request, RequestOptions.DEFAULT);
        return response.getId();
    }

    /**
     * 根据id查询文档
     * @param indexName
     * @param id
     * @return
     * @throws IOException
     */
    public String getDocById(String indexName , String id ) throws IOException {
        //设置查询的索引、文档
        GetRequest getRequest=new GetRequest(indexName,id);
        GetResponse response = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
        return  response.getSourceAsString();
    }

    /**
     * 根据id删除文档
     */
    public String delDoc(String indexName , String id ) throws IOException {
        //设置要删除的索引、文档
        DeleteRequest deleteRequest=new DeleteRequest(indexName,id);
        DeleteResponse response = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
        return  response.getId();
    }





}
